/*
 * Created by JFormDesigner on Wed Dec 25 13:55:28 ICT 2013
 */

package fashionshop.gui;

import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import fashionshop.bll.CustomerBLL;
import fashionshop.bll.ProductBLL;

/**
 * @author Hoan Tran
 */
public class CustomerAdd extends JDialog {
	
	private String customerName;
	private String customerID;
	
	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getCustomerID() {
		return customerID;
	}

	public void setCustomerID(String customerID) {
		this.customerID = customerID;
	}

	private Vector customerAddTableRecords;
	private Vector customerAddTableTitle;
	
	private void LoadCustomer() throws Exception{
		customerAddTableTitle = new Vector();
		customerAddTableRecords = new Vector();
		tableCustomerAdd.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableCustomerAdd.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "none");
		tableCustomerAdd.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "none");
		customerAddTableTitle.add("ID");
		customerAddTableTitle.add("Tên khách hàng");
		customerAddTableTitle.add("Số điện thoại");
		customerAddTableTitle.add("Địa chỉ");
		customerAddTableTitle.add("Cấp độ");
		customerAddTableTitle.add("Điểm");
		CustomerBLL customerBll = new CustomerBLL();
		customerAddTableRecords = customerBll.loadAllCustomer();
		tableCustomerAdd.setModel(new DefaultTableModel(customerAddTableRecords, customerAddTableTitle));
		
	}
	
	public CustomerAdd(Frame owner) {
		super(owner);
		initComponents();
	}

	public CustomerAdd(Dialog owner) {
		super(owner);
		initComponents();
	}

	private void btnCustomerAddSearchActionPerformed(ActionEvent e) {
		// TODO add your code here
		String keyword;
		keyword = txtCustomerAddKeyword.getText();
		CustomerBLL customerBll = new CustomerBLL();
		try {
			customerAddTableRecords = customerBll.BasicSearch(keyword);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		tableCustomerAdd.setModel(new DefaultTableModel(customerAddTableRecords, customerAddTableTitle));
	}

	private void btnCustomerAddActionPerformed(ActionEvent e) {
		// TODO add your code here
		int selectedRow = tableCustomerAdd.getSelectedRow();
        if (selectedRow != -1) {
			Vector selectedCustomer = (Vector) customerAddTableRecords.get(selectedRow);
	        this.setCustomerID(selectedCustomer.get(0).toString());
	        this.setCustomerName(selectedCustomer.get(1).toString());
	        this.dispose();        
        }
        else{
        	JOptionPane.showMessageDialog(this, "Chưa chọn khách hàng", "Lỗi", JOptionPane.ERROR_MESSAGE);
        }
	}

	private void thisWindowOpened(WindowEvent e) {
		// TODO add your code here
		try {
			this.LoadCustomer();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	private void btnProductAddSearchActionPerformed(ActionEvent e) {
		// TODO add your code here
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Hoan Tran
		scrollPane1 = new JScrollPane();
		tableCustomerAdd = new JTable();
		btnCustomerAdd = new JButton();
		label2 = new JLabel();
		txtCustomerAddKeyword = new JTextField();
		btnCustomerAddSearch = new JButton();

		//======== this ========
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				thisWindowOpened(e);
			}
		});
		Container contentPane = getContentPane();

		//======== scrollPane1 ========
		{
			scrollPane1.setViewportView(tableCustomerAdd);
		}

		//---- btnCustomerAdd ----
		btnCustomerAdd.setText("Ch\u1ecdn");
		btnCustomerAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnCustomerAddActionPerformed(e);
			}
		});

		//---- label2 ----
		label2.setText("Tu khoa");

		//---- btnCustomerAddSearch ----
		btnCustomerAddSearch.setText("Tim kiem");
		btnCustomerAddSearch.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnProductAddSearchActionPerformed(e);
				btnCustomerAddSearchActionPerformed(e);
			}
		});

		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
		contentPane.setLayout(contentPaneLayout);
		contentPaneLayout.setHorizontalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(contentPaneLayout.createSequentialGroup()
					.addGroup(contentPaneLayout.createParallelGroup()
						.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 579, Short.MAX_VALUE))
						.addGroup(contentPaneLayout.createSequentialGroup()
							.addGroup(contentPaneLayout.createParallelGroup()
								.addGroup(contentPaneLayout.createSequentialGroup()
									.addGap(273, 273, 273)
									.addComponent(btnCustomerAdd))
								.addGroup(contentPaneLayout.createSequentialGroup()
									.addGap(21, 21, 21)
									.addComponent(label2, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
									.addGap(53, 53, 53)
									.addComponent(txtCustomerAddKeyword, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
									.addGap(29, 29, 29)
									.addComponent(btnCustomerAddSearch)))
							.addGap(0, 81, Short.MAX_VALUE)))
					.addContainerGap())
		);
		contentPaneLayout.setVerticalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(contentPaneLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(contentPaneLayout.createParallelGroup()
						.addGroup(contentPaneLayout.createSequentialGroup()
							.addGap(4, 4, 4)
							.addComponent(label2))
						.addGroup(contentPaneLayout.createSequentialGroup()
							.addGap(1, 1, 1)
							.addComponent(txtCustomerAddKeyword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnCustomerAddSearch))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
					.addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 202, GroupLayout.PREFERRED_SIZE)
					.addGap(18, 18, 18)
					.addComponent(btnCustomerAdd)
					.addGap(25, 25, 25))
		);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Hoan Tran
	private JScrollPane scrollPane1;
	private JTable tableCustomerAdd;
	private JButton btnCustomerAdd;
	private JLabel label2;
	private JTextField txtCustomerAddKeyword;
	private JButton btnCustomerAddSearch;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
